package graphics.primitive;

import java.util.*;

import javax.media.opengl.*;

import graphics.engine.VertexBufferObject;


import utils.*;

public class Quad extends BaseStaticPrimitive 
{
	public static Hashtable<Integer , VertexBufferObject> vbos = new Hashtable<Integer, VertexBufferObject>();
	public static int beginMode;
	
	public Quad(GL gl) {
		super(gl);
		beginMode = GL.GL_TRIANGLES;
		
		if(!Quad.vbos.containsKey(gl.hashCode()))
		{
			Quad.vbos.put(gl.hashCode(), new VertexBufferObject());
		}
		
		this.GenerateVertices(gl);
		this.GenerateIndices(gl);
	}

	@Override
	public void GenerateVertices(GL gl) 
	{
		if(Quad.vbos.get(gl.hashCode()).VertexBufferID  != 0)
		{
			return;
		}
		ArrayList<Point3D> vertices = new ArrayList<Point3D>();
		
		vertices.add(new Point3D(-0.5f, -0.5f, 0.5f));	// 0
		vertices.add(new Point3D(0.5f, -0.5f, 0.5f));	// 1	
		vertices.add(new Point3D(-0.5f, 0.5f, 0.5f));	// 2	
		vertices.add(new Point3D(0.5f, 0.5f, 0.5f));  // 3


		Quad.vbos.get(gl.hashCode()).GenerateVertexBuffer(gl, vertices);		
	}
	
	@Override
	public void GenerateIndices(GL gl) {
		if(Quad.vbos.get(gl.hashCode()).IndexBufferID  != 0)
		{
			return;
		}
		short[] indices = new short[]
		{
			0,1,2,
			1,3,2
		};	
		
		Quad.vbos.get(gl.hashCode()).GenerateIndexBuffer(gl, indices);
	}

	/*
	public void Draw(GL gl, ProgramShader programShader)
	{
		gl.glUniformMatrix4fv(programShader.WorldMatrixUniformID, 1, false, this.WorldMatrix(camera), 0);
		
		gl.glEnableClientState(GL.GL_VERTEX_ARRAY);
		gl.glBindBuffer(GL.GL_ARRAY_BUFFER, Quad.vbo.VertexBufferID);
		gl.glVertexPointer(3, GL.GL_FLOAT, 0, 0);
		
		gl.glColor3f(this.color.R, this.color.G, this.color.B);
		
		gl.glUniform1f(programShader.isTextured, 0.0f);
		
		gl.glBindBuffer(GL.GL_ELEMENT_ARRAY_BUFFER, Quad.vbo.IndexBufferID);
		gl.glDrawElements(Quad.beginMode, Quad.vbo.indexCount, GL.GL_UNSIGNED_SHORT, 0);

		
		gl.glDisableClientState(GL.GL_VERTEX_ARRAY);
		gl.glDisableClientState(GL.GL_COLOR_ARRAY);
	}*/
}
